<?php 
Class wxmenuController extends appController
{
	
	public function index()
	{
		
		$wxmu=Model('wxmenu');
		
		
	$local_menu=$wxmu->get_wxmenu();
	
		$menu=$local_menu;
	
		
	$this->assign('view_menu',  $menu);
		$this->display('index.html');
	
	}
	public function toggle()
	{
		$result=array('error'=>0,'content'=>'');
		$cfg['status_num']=intval($_POST['status_num']);
		$cfg['id']=intval($_POST['id']);
		$cfg['table']='wxmenu';
		$cfg['name']=$_POST['name'];
		if(Model('app')->toggle($cfg))
		{
			$result['error']=0;
		}
		else
		{
			
		}
		print_r(json_encode($result));
		exit;
	
	}
	public function ordering()
	{
		$id=isset($_POST['id'])?intval($_POST['id']):'';
		$val=isset($_POST['val'])?intval($_POST['val']):'';
		$result=Model('wxmenu')->ordering($val,$id);
		print_r(json_encode($result));
	}
	function new_main_menu()
	{
		$menu=$_POST['menu'];
		$wm=Model('wxmenu');
		$result=array('error'=>'1','content'=>'');

	
		if($menu['name']=='')
		{
		
			$result['content']=Lang('NAME_EMPTY');
			die(json_encode($result));
		}
		
		
		if($menu['parent_id']==0)
		{
			if(count($wm->get_parent_menu())>2)
			{
				$result['content']='根目录菜单不能超过三个';
				die(json_encode($result));
			}
		}
		else
		{
			if(count($wm->get_child_menu($menu['parent_id']))>4)
			{
				$result['content']='子目录菜单不能超过五个';
				die(json_encode($result));
			}
		}
		if($menu['type']==1)
		{
			unset($menu['rule']);
		}
		else 
		{
			unset($menu['url']);
		}
		
		$res=$wm->insert($menu,'wxmenu');
		if($res)
		{
			$result['error']=0;
			$result['content']=Lang('SUCCESS_ADD');
		}
		else 
		{
			$result['content']=Lang('INVALID_OPERATION');
		}
		die(json_encode($result));
		
		
		
	}
	
	//显示添加按钮
	function add_main_menu()
	{
		$result=array('error'=>'0','data'=>'','cat'=>'');
		$wr=Model('wxrule');
		$wm=Model('wxmenu');
		$result['cat']=$wm->get_wxmenu(array('parent_id'=>0));
	
		//获取规则列表
		$rule_list=$wr->get_rule_list();
		if($rule_list)
		{
			foreach( $rule_list as $k=>$v)
			{
					
				$temp_list.="<option value='".$v['id']."'>".$v['name']."</option>";
					
			}
		}
		$result['data']=$temp_list;
		die(json_encode($result));
		
	}
	//删除本地
	public function del_btn()
	{
		
		$result=array('error'=>1);
		$id=isset($_REQUEST['id'])?$_REQUEST['id']:'';
		
		$res=Model('wxmenu')->del(array('id'=>$id,'table'=>'wxmenu'));
		if($res)
		{
			$result['error']=0;
		}
		print_r(json_encode($result));
	}
	//删除微信数据库上的
	public function del_menu()
	{
		$app=Model('wxmenu')->get_app();
		$token=$wxmu->getAccessToken($app['appid'], $app['appsecret']);
		//$this->weiXinObj->getToken($app['appid'], $app['appsecret']);
		$wx=new weixinController();
		$result=json_decode($wx->del_menu($token));
		$this->showMsg('删除成功');
	}
	public function refresh_menu()
	{
		$result=array('error'=>1,'content'=>'');
		$wxmu=Model('wxmenu');
		$wx=new weixinController();
		$app=$wxmu->get_app();
		$token=$wxmu->getAccessToken($app['appid'], $app['appsecret']);
		$wxmenu=$wx->get_menu($token);
		if($wxmenu)
		{
		$menu=(array)json_decode($wxmenu);
		
		$menu=$this->deformat_menu($menu);
		}
		if($wxmu->del(array('table'=>'wxmenu')))//清空数据库
		{
		    $res=$wxmu->insert_menu($menu);
		}
		
		if($res)
		{
			$result['error']=0;
			$result['content']='刷新成功';
		    
		}
		else 
		{
			$result['content']=Lang('INVALID_OPERATION');
			
		}
		$this->showMsg($result);
	}
	public function update_menu()
	{
		$menu=isset($_POST)?$_POST:'';
		$result=array('error'=>1,'conent'=>'');
		$wm=Model('wxmenu');
		
		if($menu['name']=='')
		{
		
			$result['content']=Lang('NAME_EMPTY');
			die(json_encode($result));
		}
		$old_pid=$menu['old_pid'];
		unset($menu['old_pid']);
		
		if($old_pid!=$menu['parent_id'])
		{
		  if($menu['parent_id']==0)
		 {
			if(count($wm->get_parent_menu())>2)
			{
				$result['content']='根目录菜单不能超过三个';
				die(json_encode($result));
			}
		 }
		 else
		 {
			if(count($wm->get_child_menu($menu['parent_id']))>4)
			{
				$result['content']='子目录菜单不能超过五个';
				die(json_encode($result));
			}
		 }
		}
		if($menu['type']==1)
		{
			unset($menu['rule']);
		}
		else
		{
			unset($menu['url']);
		}
		$id=$menu['id'];
		unset($menu['id']);
		$res=$wm->update($menu,array('id'=>$id,'table'=>'wxmenu'));
		if($res)
		{
			$result['error']=0;
			$result['content']=Lang('SUCCESS_edit');
		}
		else
		{
			$result['content']=Lang('INVALID_OPERATION');
		}
		
		print_r(json_encode($result));
	}
	public function edit_menu()
	{
		$id=isset($_GET['id'])?intval($_GET['id']):0;
		$wm=Model('wxmenu');
		$wxmenu=$wm->get_wxmenu_info($id);
		//父类列表
		$menu_list=$wm->get_parent_menu();
		
		//规则列表
		$rule_list=Model('wxrule')->get_rule_list();
		
		$this->assign('rule_list',  $rule_list);
		$this->assign('cat_list',  $menu_list);
		$this->assign('wxmenu',  $wxmenu);
		$this->display('wxmenu.html');
		
	}

	public function sumbit_menu()
	{
		$wx=new weixinController();
		$wxmu=Model('wxmenu');
		$app=$wxmu->get_app();
		$token=$wxmu->getAccessToken($app['appid'], $app['appsecret']);
		//$wx->getToken($app['appid'], $app['appsecret']);
		$button=array();
		$data=array();
		$menu=array();
		$json_result=array('error'=>'0','content'=>'');
		$cfg['is_show']=1;
		$temp=$wxmu->get_wxmenu($cfg);
		$temp=$this->format_menu($temp);
		
		$result=json_decode($wx->create_menu($temp,$token));
		if($result->errcode!=0)
		{
			$this->showMsg($result->errmsg);
		}
		else 
		{
			$this->showMsg($result->errmsg);
		}
		
		
	}

	private function deformat_menu($menu)
	{
		$menu=(array)$menu['menu'];
		$menu=(array)$menu['button'];
		
		$count=0;
		$new_menu=array();
		
		foreach($menu as $k=>$v)
		{
			$menu[$k]=(array)$v;
		
			if($v->sub_button)
			{
				
				  foreach($v->sub_button as $kk=>$vv)
				  {
					$menu[$k]['sub_button'][$kk]=(array)$vv;
					if($vv->type=='click')
					{
					   $menu[$k]['sub_button'][$kk]['type']=2;
					   $menu[$k]['sub_button'][$kk]['rule']=str_replace('event_', '', $menu[$k]['sub_button'][$kk]['key']);
					   unset($menu[$k]['sub_button'][$kk]['key']);
					}
					else 
					{
						$menu[$k]['sub_button'][$kk]['type']=1;
					}
				  }
				
			}
			if($v->type=='click')
			{
				$menu[$k]['type']=2;
			    $menu[$k]['rule']=str_replace('event_', '', $menu[$k]['key']);
			    unset($menu[$k]['key']);
			}
			else 
			{
				$menu[$k]['type']=1;
			}
			
		
		}
		
		
		return $menu;
		
	}
	private function format_menu($temp)
	{
		$button=array();
		$menu=array();
		foreach($temp as $k=>$v)
		{
			$menu[$k]['name']=urlencode($v['name']);
			if($v['type']==1)
			{
				$menu[$k]['type']='view';
				$menu[$k]['url']=$v['url'];
			}
			else 
			{
				$menu[$k]['type']='click';
				$menu[$k]['key']="event_".$v['rule'];
				
			}
			
			if($v['sub_button'])
			{
				
				foreach($v['sub_button'] as $kk=>$vv)
				{
				    $menu[$k]['sub_button'][$kk]['name']=urlencode($vv['name']);
			     if($vv['type']==1)
			     {
				    $menu[$k]['sub_button'][$kk]['type']='view';
				    $menu[$k]['sub_button'][$kk]['url']=$v['url'];
			    }
			     else 
			    {
				   $menu[$k]['sub_button'][$kk]['type']='click';
				   $menu[$k]['sub_button'][$kk]['key']="event_".$vv['rule'];
				
			    }
				}
			}
		}
		
		$button['button']=$menu;
		return urldecode(json_encode($button));
	}
	
	
}